//
//  HTTPTOOL.m
//  MVC练习
//
//  Created by dllo on 15/9/22.
//  Copyright (c) 2015年 _ZHW. All rights reserved.
//

#import "HTTPTOOL.h"

@implementation HTTPTOOL

+ (void)GETWithURL:(NSString *)url body:(NSDictionary *)body httpHead:(NSDictionary *)head responseStyle:(responseStyle)style success:(void(^)(id result))success fail:(void(^)(NSError *error))fail{
    /*1.创建一个网络请求管理者*/
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    /*2. 添加请求头*/
    if (head) {
        for (NSString *key in head) {
            
            [manager.requestSerializer setValue:head[key] forHTTPHeaderField:key];
        }
    }
    
    /*3.判断返回数据类型*/
    switch (style) {
        case DATA:{
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        }
        case JSON:{
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
        
            break;
        }
        case XML:{
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
        
            break;
        }
        default:
            break;
    }
    
    /*4. 设置请求接受的数据类型*/
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    
    /*5. get请求*/
    [manager GET:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        success(responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    // 1.获得网络监控的管理者
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:{ // 未知网络
                NSLog(@"未知网络");
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前为未知网络" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [alertView show];
                break;
            }
            case AFNetworkReachabilityStatusNotReachable:{ // 没有网络(断网)
//                NSLog(@"没有网络(断网)");
                UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"亲! 无网络! 请查看网络设置" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [alertView1 show];
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN:{ // 手机自带网络
//                NSLog(@"手机自带网络");
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi:{ // WIFI
//                NSLog(@"WIFI");
                break;
            }
        }
    }];
    
    // 3.开始监控
    [mgr startMonitoring];
}

/**
 *  post
 *
 *  @param url       网址
 *  @param body      请求参数
 *  @param bodyStyle bodyStyle
 *  @param head      请求头
 *  @param style     返回数据类型
 *  @param success   请求成功
 *  @param fail      请求失败
 */
+ (void)POSTWithURL:(NSString *)url body:(id)body bodyStyle:(bodyStyle)bodyStyle httpHead:(NSDictionary *)head responseStyle:(responseStyle)style success:(void (^)(id result))success fail:(void (^)(NSError *error))fail {
    
    //1.创建http请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //2.处理body类型
    switch (bodyStyle) {
        case JSONStyle: {
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        }
        case stringStyle:{
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError **error) {
                return parameters;
            }];
            break;
        }
        default: {
            break;
        }
    }
    
    //3.添加请求头
    if (head) {
        for (NSString *key in head) {
            [manager.requestSerializer setValue:head[key] forHTTPHeaderField:key];
        }
    }
    
    //4.判断返回数据类型
    switch (style) {
        case DATA:{
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        }
        case JSON:{
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            
            break;
        }
        case XML:{
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            
            break;
        }
        default:
            break;
    }

    //5.设置请求接受的数据类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    
    //6.发送POST请求
    [manager POST:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (fail) {
            fail(error);
        }
    }];
  
}

@end
